package com.ilovepostcard.view;

import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ilovepostcard.R;
import com.ilovepostcard.controller.ServerAsyncTask;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.IServer;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.DrawableManager;
import com.ilovepostcard.utils.StringUtils;
import com.ilovepostcard.utils.Utils;

public class UserProfileView extends LinearLayout{
    
    ImageView user_head_imageview;
    ImageView user_sex_mark;
    TextView user_nickname;
    TextView user_location;
    TextView user_active_status;
    TextView user_intro;
    ImageView userview_follow_button;
    TextView userview_follow_count;
    TextView userview_follow_text;

    public UserProfileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate();
    }

    public UserProfileView(Context context) {
        super(context);
        inflate();
    }
    
    public void setUser(final User user, boolean attention) {
        user_nickname.setText(user.Nickname);
        user_location.setText(StringUtils.isEmpty(user.City) ? "我在宇宙" : "我在"+user.City);
        user_active_status.setText("");
        new ServerAsyncTask(getContext()) {
            
            protected boolean isShowDialog() {
                return false;
            }
            
            @Override
            protected void onSuccess(PostcardResult result) {
                String activeTime = result.getUserActiveTime();
                
                try {
                    Date date = new Date(activeTime);
                    activeTime = date.toLocaleString();
                } catch(Exception e) {
                    
                }
                
                activeTime = activeTime.replace("T", " ");
                
                user_active_status.setText(activeTime);
            }
            
            @Override
            protected PostcardResult doRequest() {
                return ServerFactory.getServer().getUserActiveTime(user.UserID);
            }
        }.execute();
        if (user.HasHeadImg)
            DrawableManager.getInstance().fetchDrawableOnThread(user.HeadImgUrl, user_head_imageview, false);
        user_sex_mark.setImageResource(user.Sex == 2 ? R.drawable.female : R.drawable.male);
        userview_follow_button.setImageResource(attention ? R.drawable.discover_reviewpeople_button_followed : R.drawable.discover_reviewpeople_button_follow);
        userview_follow_text.setText(attention ? R.string.interested : R.string.interest);
        if (!attention) {
            userview_follow_button.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    new ServerAsyncTask(getContext()) {
                        
                        @Override
                        protected void onSuccess(PostcardResult result) {
                            userview_follow_button.setOnClickListener(null);
                            userview_follow_button.setImageResource(R.drawable.discover_reviewpeople_button_followed);
                            userview_follow_text.setText(R.string.interested);
                            userview_follow_count.setText((user.BeAttentionCount + 1)+"");
                            Toast.makeText(getContext(), R.string.success_attention, Toast.LENGTH_SHORT).show();
                        }
                        
                        @Override
                        protected PostcardResult doRequest() {
                            User loginUser = UserController.getLoginUser(getContext());
                            return ServerFactory.getServer().attention(loginUser.UserID, user.UserID, IServer.ATTENTION_TYPE_FOLLOW);
                        }
                    }.execute();
                }
            });
        }
        userview_follow_count.setText(user.BeAttentionCount+"");
        String intro = user.Introduction;
        if (StringUtils.isBlank(intro)) {
            intro = Utils.randomIntroduction(getContext());
        }
        user_intro.setText(intro);
    }

    private void inflate() {
        View view = View.inflate(getContext(), R.layout.user_view, this);
        user_head_imageview = (ImageView) view.findViewById(R.id.user_head_imageview);
        user_sex_mark = (ImageView) view.findViewById(R.id.user_sex_mark);
        user_nickname = (TextView) view.findViewById(R.id.user_nickname);
        user_location = (TextView) view.findViewById(R.id.user_location);
        user_active_status = (TextView) view.findViewById(R.id.user_active_status);
        user_intro = (TextView) view.findViewById(R.id.user_intro);
        userview_follow_button = (ImageView) view.findViewById(R.id.userview_follow_button);
        userview_follow_count = (TextView) view.findViewById(R.id.userview_follow_count);
        userview_follow_text = (TextView) view.findViewById(R.id.userview_follow_text);
    }
}
